﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using CRMEntity;
using CRMBiz;

namespace CRMConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            List<FollowUpContact> contactList = null;

            try
            {
                contactList = ContactFollowUpHandler.GetFollowUpList();

                if (contactList!=null && contactList.Count() > 0)
                {
                    DoFollowUpOperation(contactList);
                }
            }
            catch (Exception ex)
            {
                WriteLogFile(ex.Message);
            }
        }

        private static void DoFollowUpOperation(List<FollowUpContact> contactList)
        {
            ContactFollowUpHandler.Setup();

            int index = 1;
            var followUpList = from c in contactList where !c.IsEmailNull select c;
            string smsFlag = null;

            if (followUpList.Count() > 0)
            {
                Console.WriteLine("Sending following up email is starting...");

                foreach (FollowUpContact c in followUpList)
                {
                    try
                    {
                        if (!string.IsNullOrEmpty(c.MobileNumber))
                        {
                            smsFlag = ContactFollowUpHandler.SendMessage(c);
                            WriteLogFile("sending sms status" + smsFlag);
                        }

                        if (IsValidEmailAddress(c.EmailAddress))
                        {
                            Console.WriteLine("Sending follow up Email and SMS {0} of {1}...", index, followUpList.Count());
                            ContactFollowUpHandler.SendEmail(c);
                        }
                        else
                        {
                            WriteLogFile("***Error*** Contact ID: " + c.ContactId.ToString() + " has invalid email address " + c.EmailAddress);
                        }

                        index += 1;
                        //  WriteLogFile("************success");
                    }
                    catch (Exception ex)
                    {
                        WriteLogFile("Application no: " + c.ApplicationId.ToString() + " " + ex.Message);
                    }
                }
                Console.WriteLine("Sending following up Email and SMS is finished.");
            }
        }
        /// <summary>
        /// Write error message to log file while error happened
        /// </summary>
        /// <param name="followUpInfo"></param>
        /// <param name="errorMessage"></param>
        private static void WriteLogFile(string errorMessage)
        {
            using (var writer = new StreamWriter(new FileStream("SendFollowUpErrorLog.txt", FileMode.Append)))
            {
                writer.WriteLine("Time: {0}  Error: {1}", DateTime.Now.ToString(), errorMessage);
            }
        }
        /// <summary>
        /// Check client email address is valid
        /// </summary>
        /// <param name="emailAddress"></param>
        /// <returns></returns>
        private static bool IsValidEmailAddress(string emailAddress)
        {
            bool isValid = true;
            try
            {
                var validEmailAddress = new System.Net.Mail.MailAddress(emailAddress);
            }
            catch
            {
                isValid= false;
            }
            return isValid;
        }
    }
}
